package com.example.gokchinesefoodmappcdev.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.gokchinesefoodmappcdev.entity.*;
import com.example.gokchinesefoodmappcdev.entity.dto.GoodsDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.GoodsInfoExcelDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.OrderInfoDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.OrderInfoExcelDTO;
import com.example.gokchinesefoodmappcdev.service.OrderInfoService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest")

public class OrderController {

    @Autowired
    OrderInfoService orderInfoService;

    /*
    * 根据分页信息，查询出订单列表
    * */
    @RequestMapping("/order/list")
    public Map<String,Object> orderList(Integer pageNumber, Integer pageSize){
        ResultData resultData = orderInfoService.selectOrderList(pageNumber, pageSize);
        return resultData;

    }

    /*
     * •	根据传入的订单号查询普通订单详情
     * */
    @RequestMapping("/order/info")
    public Map<String,Object> orderList(String orderNo){

        int addressId = orderInfoService.selectAddressIdByOrderNo(orderNo);
        ResultData resultData = orderInfoService.selectOrderInfoSByAddressAndOrderNo(addressId,orderNo);

        return resultData;

    }

    /*
     * •根据订单号查询子订单详情
     * */
    @RequestMapping("/order/listSubOrder")
    public Map<String,Object> listSubOrder(String orderNo){

        ResultData resultData = orderInfoService.selectListSubOrder(orderNo);
        return resultData;
    }

    /*
     * •订单发货
     *     根据订单号，修改发货状态（2：已发货），发货时间，填上物流编号，物流公司，
     * */
    @RequestMapping("/order/delivery")
    public Map<String,Object> deliveryOrder(String orderNo,String logisticsNumber,String logisticsCompany){

        ResultData resultData = orderInfoService.deliveryOrder(orderNo, logisticsNumber, logisticsCompany);
        return resultData;
    }

    /*
     * 商户端普通订单导出
     *      需要新建一个DTO，把要查询的字段放进去，然后新建一个Excel实体，和DTO放一样的字段，
     *        并且用@ExcelProperty(value = {"订单编号"}, index = 0)这种标注每一个字段，
     *         然后再去数据库查询数据，返回
     *
     * */
    @RequestMapping("/excel/orderList")
    public Map<String,Object> orderListToExcel(){

        ExcelWriter writer = null;
        OutputStream out = null;
        try {
            List<OrderInfoExcelDTO> ordersInfo = orderInfoService.getOrdersInfo();
            String fileName = "订单信息表格";
            out = new FileOutputStream("F:\\var\\" + fileName + ".xlsx");
            writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            Sheet sheet = new Sheet(1, 0, OrderInfoExcel.class);
            sheet.setSheetName("订单信息");
            writer.write(ordersInfo, sheet);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.finish();
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ResultData resultData = new ResultData(0, "表格生成成功");
        resultData.put("path","F\\var");
        return resultData;
    }

    /*
     * ••	普通订单退款分页查询
     * */
    @RequestMapping("/orderReturn/list")
    public Map<String,Object> orderReturnList(Integer pageSize,Integer pageNumber,String returnNo){

        ResultData resultData = orderInfoService.selectOrderReturnList(pageSize, pageNumber, returnNo);


        return resultData;
    }
}
